{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import copy\n",
    "import requests\n",
    "from requests.adapters import HTTPAdapter\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import dashscope\n",
    "from requests.adapters import HTTPAdapter\n",
    "from http import HTTPStatus\n",
    "def call_embedding(prompt,url = \"http://127.0.0.1:5555/embedding\"):\n",
    "    \n",
    "    headers = {\"Content-Type\":\"application/json\"}\n",
    "    data = json.dumps({\"prompt\":prompt})\n",
    "    s = requests.Session()\n",
    "    s.mount('http://',HTTPAdapter(max_retries = 3))\n",
    "    try:\n",
    "        res = s.post(url,data=data,headers = headers,timeout = 600)\n",
    "        # print(res)\n",
    "        if res.status_code ==200:\n",
    "            return res.json()['response']\n",
    "        else:\n",
    "            return None\n",
    "    except requests.exceptions.RequestException as e:\n",
    "        print(e)\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = call_embedding('你是谁？')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_rerank(prompt,url = \"http://127.0.0.1:9999/rerank\"):\n",
    "    headers = {\"Content-Type\":\"application/json\"}\n",
    "    data = json.dumps({\"prompt\":prompt})\n",
    "    s = requests.Session()\n",
    "    s.mount('http://',HTTPAdapter(max_retries = 3))\n",
    "    try:\n",
    "        res = s.post(url,data=data,headers = headers,timeout = 600)\n",
    "        # print(res)\n",
    "        if res.status_code ==200:\n",
    "            return res.json()['response']\n",
    "        else:\n",
    "            return None\n",
    "    except requests.exceptions.RequestException as e:\n",
    "        print(e)\n",
    "        return None\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "elderberry: 5\n",
      "date: 4\n",
      "cherry: 3\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'apple': 1, 'banana': 2, 'cherry': 3, 'date': 4, 'elderberry': 5}\n",
    "\n",
    "# 定义你想要显示的元素数量\n",
    "n = 3\n",
    "\n",
    "# 使用sorted()函数对字典的items()进行排序，这里按键排序，如果不关心顺序也可以直接取前n个\n",
    "sorted_items = sorted(my_dict.items(),key=lambda x:x[1],reverse=True)\n",
    "\n",
    "# 使用切片来获取前n个元素\n",
    "first_n_items = sorted_items[:n]\n",
    "\n",
    "# 打印前n个元素\n",
    "for key, value in first_n_items:\n",
    "    print(f\"{key}: {value}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur_text = []\n",
    "cur_text+=[{1:[0,1,2,3]}]\n",
    "cur_text+=[{1:[0,1,12,43]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{1: [0, 1, 2, 3]}, {1: [0, 1, 12, 43]}]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cur_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "end = []\n",
    "for item in cur_text:\n",
    "    print(list(item.keys())[0])\n",
    "    # end+=list(item.values())[0]\n",
    "# end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 0, 1, 12, 43]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [0, 1, 2, 3]\n",
    "a+=[4, 5, 6, 7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
